package businessLayer.businessObjects;

import java.util.ArrayList;

import dataAccessLayer.abstractFactory.Factory;
import dataAccessLayer.abstractFactory.daos.DAOcities;
import dataAccessLayer.abstractFactory.daos.DAOusers;
import dataAccessLayer.visualObject.VOcity;
import dataAccessLayer.visualObject.VOuser;

public class UsersBusinessObject 
{	
	private DAOusers daoUsers;
	private DAOcities daoCities;
	
	private int currentUserType;
	private String currentUserName;
	private int currentUsercode;

	public UsersBusinessObject(Factory factory) 
	{
		daoUsers = factory.getDAOusers();
		daoCities = factory.getDAOcities();
	}

	public boolean authenticUser(String userName, String password) 
	{
		VOuser authenticUser = new VOuser();
		authenticUser.setUserName(userName);
		authenticUser.setPassword(password);
		
		VOuser resul = null;
		
		resul = daoUsers.authenticUser(authenticUser);
		
		if(resul==null)
		{
			return false;
		}
		
		resul = daoUsers.searchUserByCode(resul);
		currentUserType = resul.getUserType();
		currentUserName = resul.getUserName();
		currentUsercode = resul.getCode();
		
		return true;
	}

	public int getCurrentUserType() 
	{
		return currentUserType;
	}
	
	public String getCurrentUserName() 
	{
		return currentUserName;
	}

	public Object[][] getUsers() 
	{
		ArrayList<VOuser> users = null;
		
		users = daoUsers.listUsers();
		
		Object[][] resul = new Object[users.size()][8];
		String[] cities = new String[users.size()];
		
		VOcity vOcity = new VOcity();
		
		for (int i = 0; i < cities.length; i++) 
		{
			vOcity.setCode(users.get(i).getCity());
			
			cities[i] = (daoCities.searchCityByCode(vOcity)).getName();
		}
		
		for (int i = 0; i < cities.length; i++) 
		{
			for (int j = 0; j < 8; j++) 
			{
				switch(j)
				{
				case 0:
					resul[i][j] = users.get(i).getFirstName();
					break;
				case 1:
					resul[i][j] = users.get(i).getLastName();
					break;
				case 2:
					resul[i][j] = users.get(i).getIdentification();
					break;
				case 3:
					resul[i][j] = users.get(i).getAddress();
					break;
				case 4:
					resul[i][j] = cities[i];
					break;
				case 5:
					resul[i][j] = users.get(i).getTelephone();
					break;
				case 6:
					resul[i][j] = users.get(i).getUserName();
					break;
				case 7:
					resul[i][j] = solverUserType(users.get(i).getUserType());
					break;
				}
			}
		}
		
		return resul;
	}

	private Object solverUserType(int userType) 
	{
		String resul = null;
		switch (userType)
		{
		case 1:
			resul = "Administrador";
			break;
		case 2:
			resul = "Pastelero";
			break;
		case 3:
			resul = "Vendedor";
			break;
		}
		return resul;
	}

	public String[] getCities() 
	{
		ArrayList<VOcity> cities = daoCities.listCities();
		
		String[] resul = new String[cities.size()];
		
		for (int i = 0; i < resul.length; i++) 
		{
			resul[i] = cities.get(i).getName();
		}
		
		return resul;
	}

	public boolean insertCity(String newCity) 
	{
		VOcity insertCity = new VOcity();
		insertCity.setName(newCity);
		
		int code = daoCities.insertCity(insertCity);
		
		if(code>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public boolean insertUser(String firstName, String lastName,int identification, String address, int city, String telephone,String userName, String password, int userType) 
	{
		VOuser vOuser = new VOuser();
		vOuser.setFirstName(firstName);
		vOuser.setLastName(lastName);
		vOuser.setIdentification(identification);
		vOuser.setAddress(address);
		vOuser.setCity(city);
		vOuser.setTelephone(telephone);
		vOuser.setUserName(userName);
		vOuser.setPassword(password);
		vOuser.setUserType(userType);
		
		int code = daoUsers.insertUser(vOuser);
		
		if(code>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public int getCurrentUserCode() 
	{
		return currentUsercode;
	}

}
